// 入口文件
//导入Vue
import Vue from 'vue'

//导入mui定义tabba
import './lib/mui/css/mui.min.css'
//导入字体图标定义购物车图标
import './lib/mui/css/icons-extra.css'
//按需导入mint-ui定义Header 轮播图
// import { Header,Swipe, SwipeItem ,Button ,Lazyload} from 'mint-ui'
// Vue.component(Header.name,Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Button.name, Button);
// Vue.use(Lazyload);
import MintUI from 'mint-ui';
Vue.use(MintUI);
import 'mint-ui/lib/style.css'

// import VuePreview from 'vue-preview'
// // defalut install
// Vue.use(VuePreview)
// Vue.use(preview, {
//     mainClass: 'pswp--minimal--dark',
//     barsSize: {top: 0, bottom: 0},
//     captionEl: false,
//     fullscreenEl: false,
//     shareEl: false,
//     bgOpacity: 0.85,
//     tapToClose: true,
//     tapToToggleControls: false
//   })

//导入App首页
import app from './App.vue'
//导入路由模块定义中间及底部区域
import VueRouter from "vue-router"
Vue.use(VueRouter)
//导入自己的路由模块
import router from './router.js'
//导入 vue-resource
import VueResource from 'vue-resource'
Vue.use(VueResource)
//设置请求的根路径
Vue.http.prototype.option="http://vue.studyit.io";
// Vue.http.option.emulateJSON=true;
import moment from 'moment'

Vue.filter('dateFormat',function(dataStr,pattern="YYYY-MM-DD HH:mm:ss"){
    return moment(dataStr).format(pattern)
})


//注册vuex
import Vuex from 'vuex'
Vue.use(Vuex)
var car= JSON.parse(localStorage.getItem('car')||'[]')
var store=new Vuex.Store({
    state:{//this.$store.state.***
        car:car
    },
    mutations:{//this.$store.commit('方法名称','按需导入的唯一参数')
        addToCar(state,goodsInfo){
            var flag=false;
            state.car.some(item =>{
                if(item.id==goodsInfo.id){
                    item.count+=parseInt(goodsInfo.count)
                    flag=true
                    return true
                }
            })
            if(!flag){
                state.car.push(goodsInfo)
            }
            localStorage.setItem('car',JSON.stringify(state.car))
        },
        updateGoodsInfo(state,goodsInfo){
            state.car.some(item=>{
                if(item.id==goodsInfo.id){
                    item.count=parseInt(goodsInfo.count);
                    return true;
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car)) 
        },
        updateGoodsSelected(state,info){
            state.car.forEach(item=>{
                if(item.id==info.id){
                    item.selected=info.selected;
                }
            })
            localStorage.setItem('car',JSON.stringify(state.car)) 
        }
    },
    getters:{//this.$store.getters.***
        getAllCount(state){
            var c=0;
            state.car.forEach(item =>{
                c+=item.count
            })
            return c
        },
        getGoodsCount(state){
            var o={}
            state.car.forEach(item =>{
                o[item.id]=item.count;
            })
            return o;
        },
        getGoodsSelected(state){
            var o={};
            state.car.forEach(item =>{
                o[item.id]=item.selected;
            })
            return o;
        },
        getGoodsCountAndAmount(state){
            var o={
                count:0,
                amount:0
            }
            state.car.forEach(item =>{
                if(item.selected){
                    o.count+=item.count;
                    o.amount+=item.count*5488
                }
            })
        }
    }
})

var vm =new Vue({
    el:'#app',
    render:c => c (app),
    router,
    store//数据共享仓库
})